var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
const session = require('express-session');
const nunjucks = require('nunjucks');
const sqlite3 = require('sqlite3').verbose();

var blogRouter = require("./routes/blog");
var authRouter = require("./routes/auth");

var app = express();

// 将数据库连接注册为全局变量
const db = new sqlite3.Database("./instance/expresser.sqlite");
app.locals.db = db;

// 配置 session 中间件
app.use(
  session({
    secret: "dev",
    resave: false,
    saveUninitialized: true,
  })
);

// view engine setup
nunjucks.configure("views", {
  autoescape: true,
  express: app,
});
app.set("view engine", "html");

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", blogRouter);
app.use("/auth", authRouter);

module.exports = app;
